#ifndef TRDPTHREAD_H
#define TRDPTHREAD_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>

#if defined (POSIX)
#include <unistd.h>
#include <sys/select.h>
#include <sys/syscall.h>
#elif (defined (WIN32) || defined (WIN64))
#include "getopt.h"
#endif

#ifdef RUN_IN_BOARD
#include "tlc_if.h"
#include "vos_thread.h"
#include "vos_utils.h"
#endif

#include "globalconfig.h"

/* We use static memory    */
#define RESERVED_MEMORY  1000000u
#define MAX_PD_PORT_NUM 128

typedef struct
{
    TRDP_SUB_T      subHandle;                      /* subscribe handle */
    UINT32          comID;                          /* comid            */
    TRDP_IP_ADDR_T  src;                            /* source ip address               */
    TRDP_IP_ADDR_T  dst;                            /* destination ip address          */
    UINT32          cycle;                          /* cycle                                */
    UINT32          timeout;                        /* timeout (for SINK ports)             */
    UINT8           data[TRDP_MAX_PD_DATA_SIZE];    /* data buffer                          */
    int             link;                           /* index of linked port (echo or subscribe) */
    UINT16          comidIndex;
    UINT32          receivedSize;
    TRDP_PD_INFO_T pdi;
} PD_RECEIVE_PACKET_T;

typedef struct
{
    TRDP_PUB_T      pubHandle;
    UINT32          comID;
    UINT32          interval;
    TRDP_IP_ADDR_T  src;
    TRDP_IP_ADDR_T  dst;
    UINT8           data[TRDP_MAX_PD_DATA_SIZE];
    UINT32          sendSize;
    int             changed;
    int             invalid;
} PD_SEND_PACKET_T;

class TRDPThread
{
public:
    TRDPThread();
    ~TRDPThread();
    enum ComState { initial, running, stop, error };
    void initCom();
    int startCom();
    void stopCom();
    int getComState();
    const unsigned char* getRecvPointer(int comindex);
    unsigned char* getSendPointer(int comindex);
    int getRecvSize();
    int getSendSize();
    unsigned int getRecvStartByte(int index);
    unsigned int getSendStartByte(int index);
    pthread_mutex_t trdp_send_mutex;
//TRDP
private:
    static void dbgOut(void *pRefCon,
                       TRDP_LOG_T  category,
                       const CHAR8 *pTime,
                       const CHAR8 *pFile,
                       UINT16      LineNumber,
                       const CHAR8 *pMsgStr);
        TRDP_APP_SESSION_T      appHandle;
        TRDP_ERR_T              err;
        UINT32                  ownIP;
        TRDP_PD_CONFIG_T        pdConfiguration =
        {NULL, NULL, TRDP_PD_DEFAULT_SEND_PARAM, TRDP_FLAGS_NONE, 1000000u, TRDP_TO_SET_TO_ZERO, 17224};
        TRDP_MEM_CONFIG_T       dynamicConfig   = {NULL, RESERVED_MEMORY, {0}};
        TRDP_PROCESS_CONFIG_T   processConfig   = {"Me", "", TRDP_PROCESS_DEFAULT_CYCLE_TIME, 0, TRDP_OPTION_NONE};
        PD_RECEIVE_PACKET_T gRecvPacket[MAX_PD_PORT_NUM];
        PD_SEND_PACKET_T gSendPacket[MAX_PD_PORT_NUM];
        int gRecvPortCnt=0;
        int gSendPortCnt=0;
        int RecvDataSize=0;
        int SendDataSize=0;
        void pd_suscribe(TRDP_APP_SESSION_T &apph,PD_RECEIVE_PACKET_T *pd_recv_pack);
        void pd_publish(TRDP_APP_SESSION_T &apph,PD_SEND_PACKET_T *pd_send_pack);
        void add_pd_sinkPort(TRDPDataPortInfo &port);
        void add_pd_sourcePort(TRDPDataPortInfo &port);
        static void* comThread(void* arg);
//DDU
private:
        ComState m_state;
};

#endif // TRDPTHREAD_H
